package cn.itsource.org.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.common.exception.BusinessException;
import cn.itsource.common.util.AjaxResult;
import cn.itsource.common.util.BaiduAiUtil;
import cn.itsource.common.util.EmailUtil;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Logininfo;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.LogininfoMapper;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.myenum.ShopState;
import cn.itsource.org.ro.EmployeeRo;
import cn.itsource.org.ro.ShopAuditLogRo;
import cn.itsource.org.ro.ShopRo;
import cn.itsource.org.service.IShopService;
import cn.itsource.common.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    //完成自己的业务
    @Override
    public AjaxResult settlement(ShopRo shopRo) {
//        BaiduAiUtil.TextCensor(shopRo.getName());
//        BaiduAiUtil.TextCensor(shopRo.getAddress());

        //密码是否一致
        String password = shopRo.getAdmin().getPassword();
        String comfirmPassword = shopRo.getAdmin().getComfirmPassword();
        if(!password.equals(comfirmPassword)){
            //return AjaxResult.me().setSuccess(false).setMessage("两次密码不一致！");
            throw new BusinessException("两次密码不一致！");
        }
        //验证你的公司名 是否存在
        //手机号
        //邮箱
        //地址

        Shop shop = shopMapper.findByName(shopRo.getName());
        Employee employee = employeeMapper.findByPhone(shopRo.getAdmin().getPhone());
        if(shop == null && employee == null){
            //保存employee
            EmployeeRo admin = shopRo.getAdmin();
            Employee employee1 = EmployeeRo.EmployeeRotoEmployee(admin);
            //密码加密
            String salt = RandomUtil.randomString(32);
            employee1.setSalt(salt);
            String newPassword = SecureUtil.md5(salt + employee1.getPassword());
            employee1.setPassword(newPassword);
            employeeMapper.save(employee1);
            //才可以添加数据
            //保存 shop  但是 admin_id 没有
            Shop shop1 = ShopRo.ShopRotoShop(shopRo);
            shop1.setAdminId(employee1.getId());
            shop1.setRegisterTime(new Date());
            shop1.setState(ShopState.ONE.getCode());
            shopMapper.save(shop1);
            employee1.setShop_id(shop1.getId());
            employeeMapper.update(employee1);
        }else {
            return AjaxResult.me().setSuccess(false).setMessage("店铺或者用户已存在！");
        }
        //你有几张表要保存
        return AjaxResult.me();
    }

    @Override
    @Transactional
    public void pass(ShopAuditLogRo shopAuditLogRo) {
        //当前数据状态要修改
        Shop shop = shopMapper.findById(shopAuditLogRo.getShopId());
        shop.setState(ShopState.TWO.getCode());
        shopMapper.update(shop);
        Employee employee = employeeMapper.findById(shop.getAdminId());
        //初始化一个账号
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setDisable(false);//未激活状态
        logininfo.setType(0);
        logininfoMapper.save(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.update(employee);//是为了保存Logininfo_id 我们在后面可以根据Logininfo_id找店铺
        //发送邮件   激活
        //发送的内容  note
        String note = shopAuditLogRo.getNote()+"<a href='http://127.0.0.1:8080/logininfo/active/"+logininfo.getId()+"'>点击激活</a>";
        emailUtil.send2(note,employee.getEmail());
        //保存日志 单独一张表
        //自己去做
        ShopAuditLog shopAuditLog = new ShopAuditLog();
        shopAuditLog.setNote(note);
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setShopId(shopAuditLogRo.getShopId());
        shopAuditLog.setState(ShopState.TWO.getCode());
        shopAuditLogMapper.save(shopAuditLog);
    }

    @Override
    public void reject(ShopAuditLogRo shopAuditLogRo) {
        //当前数据状态要修改
        Shop shop = shopMapper.findById(shopAuditLogRo.getShopId());
        shop.setState(ShopState.FOUR.getCode());
        shopMapper.update(shop);
        //发送邮件告诉用户数据问题
        Employee employee = employeeMapper.findById(shop.getAdminId());
        //发送的内容  note
        emailUtil.send2(shopAuditLogRo.getNote(),employee.getEmail());
        //保存日志 单独一张表
        //自己去做
    }
}
